import Stack from "./stack-array";

/**
 * 进制转换 2 - 32
 * @param decNumber int  转换数字 (10进制)
 * @param base int 进制
 */
function baseConverter(decNumber: number, base: number): string | undefined {
  try {
    if (base < 2 || base > 32) {
      throw new Error("进制需要在2 - 32之间");
    }
    if (!/^\d+$/.test(decNumber.toString())) {
      console.warn("传入的不是整数, 小数点后会被丢弃");
    }
    const bigits: string = "0123456789ABCDEFGHIJKLMNOPQRESTUVWXYZ";
    // decNumber需要是整数 防止传入小数需要取整
    let num: number = Math.floor(decNumber);
    const baseStack: Stack<string> = new Stack<string>();
    let rem: string;
    while (num > 0) {
      // 取余获取余数
      rem = bigits[num % base];
      baseStack.push(rem);
      num = Math.floor(num / base);
    }

    let r_str: string = "";
    while (baseStack.size()) {
      const len = baseStack.size();
      // if(len %  4 === 0){
      //     r_str+=" "
      // }
      r_str += baseStack.pop();
    }

    return r_str;
  } catch (e) {
    console.error(e);
  }
}

console.log(baseConverter(1024.321312, 332));
